// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adwords.axis.v201109.basicoperations;

import com.google.api.ads.adwords.axis.v201109.cm.AdGroup;
import com.google.api.ads.adwords.axis.v201109.cm.AdGroupOperation;
import com.google.api.ads.adwords.axis.v201109.cm.AdGroupReturnValue;
import com.google.api.ads.adwords.axis.v201109.cm.AdGroupServiceInterface;
import com.google.api.ads.adwords.axis.v201109.cm.AdGroupStatus;
import com.google.api.ads.adwords.axis.v201109.cm.Bid;
import com.google.api.ads.adwords.axis.v201109.cm.ManualCPMAdGroupBids;
import com.google.api.ads.adwords.axis.v201109.cm.Money;
import com.google.api.ads.adwords.axis.v201109.cm.Operator;
import com.google.api.ads.adwords.lib.client.AdWordsSession;
import com.google.api.ads.adwords.lib.factory.AdWordsServices;
import com.google.api.ads.common.lib.auth.ClientLoginTokens;

/**
 * This example adds ad groups to a campaign. To get campaigns, run
 * GetCampaigns.java.
 *
 * Credentials and properties in {@code fromFile()} are pulled from the
 * "ads.properties" file. See README for more info.
 *
 * Tags: AdGroupService.mutate
 *
 * @author Kevin Winter
 */
public class AddCPMAdGroups {

  public static void main(String[] args) throws Exception {
    // Get a ClientLogin AuthToken.
    String clientLoginToken = new ClientLoginTokens.Builder()
        .forApi(ClientLoginTokens.Api.ADWORDS)
        .fromFile()
        .build()
        .requestToken();

    // Construct an AdWordsSession.
    AdWordsSession session =
        new AdWordsSession.Builder().fromFile().withClientLoginToken(clientLoginToken).build();

    long campaignId = Long.parseLong("INSERT_CAMPAIGN_ID_HERE");

    runExample(session, campaignId);
  }

  public static void runExample(AdWordsSession session, long campaignId) throws Exception {
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService =
        new AdWordsServices().get(session, AdGroupServiceInterface.class);

    // Create ad group.
    AdGroup adGroup = new AdGroup();
    adGroup.setName("Earth to Mars Cruises #" + System.currentTimeMillis());
    adGroup.setStatus(AdGroupStatus.ENABLED);
    adGroup.setCampaignId(campaignId);

    // Create ad group bid.
    ManualCPMAdGroupBids adGroupBids = new ManualCPMAdGroupBids();
    adGroupBids.setMaxCpm(new Bid(new Money(null, 10000000L)));
    adGroup.setBids(adGroupBids);

    // Add as many additional ad groups as you need.
    AdGroup adGroup2 = new AdGroup();
    adGroup2.setName("Earth to Venus Cruises #" + System.currentTimeMillis());
    adGroup2.setStatus(AdGroupStatus.ENABLED);
    adGroup2.setCampaignId(campaignId);

    ManualCPMAdGroupBids adGroupBids2 = new ManualCPMAdGroupBids();
    adGroupBids2.setMaxCpm(new Bid(new Money(null, 20000000L)));
    adGroup2.setBids(adGroupBids2);

    // Create operations.
    AdGroupOperation operation = new AdGroupOperation();
    operation.setOperand(adGroup);
    operation.setOperator(Operator.ADD);
    AdGroupOperation operation2 = new AdGroupOperation();
    operation2.setOperand(adGroup2);
    operation2.setOperator(Operator.ADD);

    AdGroupOperation[] operations = new AdGroupOperation[] {operation, operation2};

    // Add ad groups.
    AdGroupReturnValue result = adGroupService.mutate(operations);

    // Display new ad groups.
    for (AdGroup adGroupResult : result.getValue()) {
      System.out.println("Ad group with name \"" + adGroupResult.getName() + "\" and id \""
          + adGroupResult.getId() + "\" was added.");
    }
  }
}
